<?php
	require_once("baseobj.php");

class TaskPriority extends BaseObj {
	public $TaskPriorityId = 0;
	public $Name = "";
	public $Description = "";
	public $Position = 0;
	public static $Select = "SELECT * FROM TaskPriority";
	
	public function __construct() { }
	
	public function delete() {
		$sql = "DELETE FROM TASKPRIORITY WHERE TaskPriorityId = :tpid";
		$conn = createConn();
		$stmt = oci_parse($conn, $sql);
		oci_bind_by_name($stmt, ':tpid', $this->TaskPriorityId);
		$cnt = oci_execute($stmt);
		oci_free_statement($stmt);
		oci_close($conn);
	}
	
	public static function getAll(){
		$arr = array();
		$i = 0;
		$sql = TaskPriority::$Select." ORDER BY Name";
		$conn = createConn();
		$stmt = oci_parse($conn, $sql);
		$cnt = oci_execute($stmt);
		$r = null;
		if($cnt > 0) {
			while($row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS)) {
				$r = new TaskPriority();
				$r->load($row);
				$arr[$i] = $r;
				$i++;
			}
		}			
		oci_free_statement($stmt);
		oci_close($conn);
		
		return $arr;
	}
	
	public static function getByTaskPriorityId($taskpriorityid){
			$sql = TaskPriority::$Select."  WHERE TaskPriorityId = :TaskPriorityId";
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':TaskPriorityId', $taskpriorityid);

			$cnt = oci_execute($stmt);
			$u = null;
			if($cnt > 0) {
				$row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);				
				$u = new TaskPriority();
				$u->load($row);
			}			
			oci_free_statement($stmt);
			oci_close($conn);
			
			return $u;
	}
	
	public static function getFromURL(){
		$id = null;
		$obj = null;
		if(isset($_GET["taskpriorityid"])){ 
			$id = $_GET["taskpriorityid"];
			$obj = TaskPriority::getByTaskPriorityId($id);
		}
		return $obj;
	}
	
	public function load($row){			
		$this->TaskPriorityId = $row["TASKPRIORITYID"];
		$this->Name = $row["NAME"];
		$this->Description = $row["DESCRIPTION"];
		$this->Position = $row["POSITION"];
	}
	
	public static function nextId() {
			$sql = "SELECT seq.nextval FROM Dual"; 
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_execute($stmt);
			$row = oci_fetch_array($stmt, OCI_ASSOC+OCI_RETURN_NULLS);
			$v = $row["NEXTVAL"];
			oci_free_statement($stmt);
			oci_close($conn);
			return $v;
		}
	
	public function save() { 
		$sql = null;
			if($this->TaskPriorityId == null || $this->TaskPriorityId == 0) {
				$this->TaskPriorityId = TaskPriority::nextId();
				$sql = "INSERT INTO TaskPriority (TaskPriorityId, Name, Description, Position) 
				        VALUES (:TaskPriorityId, :Name, :Description, :Position)";	
			}
			else {  				
				$sql = "UPDATE TaskPriority 
						SET Name = :Name, 
							Description = :Description,
							Position = :Position
						WHERE TaskPriorityId = :TaskPriorityId";	
			}
			$conn = createConn();
			$stmt = oci_parse($conn, $sql);
			oci_bind_by_name($stmt, ':TaskPriorityId', $this->TaskPriorityId);
			oci_bind_by_name($stmt, ':Name', $this->Name);
			oci_bind_by_name($stmt, ':Description', $this->Description);
			oci_bind_by_name($stmt, ':Position', $this->Position);
			$cnt = oci_execute($stmt);
			oci_free_statement($stmt);
			oci_close($conn);
			
			return $cnt > 0;
	}
}
?>